package servlets;

import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;

public class SomeServlet implements Servlet {

    private ServletConfig config;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.config = servletConfig;
        System.out.println("config = "+servletConfig);
    }

    @Override
    public ServletConfig getServletConfig() {
        return config;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        //获取Servlet的名称
        String servletName = config.getServletName();
        System.out.println("servletName = "+ servletName);

        //获取ServletContext对象
        ServletContext servletContext = config.getServletContext();
        System.out.println("servletContext = " + servletContext);


        //获取所有的初始化参数
        Enumeration<String> names = config.getInitParameterNames();
        //遍历枚举
        while (names.hasMoreElements()) {
            //获取枚举的当前遍历的值
            String name = names.nextElement();

            //获取指定参数的值
            String value = config.getInitParameter(name);
            System.out.println(name + " = " + value);
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
